package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void update(Role role) {
        roleDao.updateById(role);
    }

    @Override
    public void delete(String id) {
        roleDao.deleteById(id);
    }

    @Override
    public PageInfo findPage(String companyId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List list = roleDao.findAll(companyId);
        return new PageInfo(list,5);
    }

    @Override
    public void updateRoleModule(String roleid, String moduleIds) { //moduleIds ="1,3,556,67,89"
//        先删除  就是把这个角色下之前的菜单权限全部删除
//          delete from pe_role_module where role_id=#{roleid}
        roleDao.deleteRoleAndModuleByRoleId(roleid);
        if(StringUtils.isNotEmpty(moduleIds)){
            String[] mIds = moduleIds.split(",");
            for (String mId : mIds) {
                roleDao.saveRoleAndModule(roleid,mId);
            }
//        insert into pe_role_module(role_id,module_id) values (#{roleid},#{moduleid});
        }
    }

    @Override
    public List<String> findModuleLisByRoleId(String roleid) {
        return roleDao.findModuleLisByRoleId(roleid);
    }
}
